package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import cn.sumpu.app.guoguojie.dao.RushDao;
import cn.sumpu.app.guoguojie.domain.Rush;
import cn.sumpu.app.guoguojie.domain.RushPhone;
import cn.sumpu.app.guoguojie.domain.RushPlan;
import cn.sumpu.app.guoguojie.domain.RushTime;
import cn.sumpu.app.guoguojie.domain.RushTimePart;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class RushDaoImpl extends SqlSessionDaoSupport implements RushDao{
	
	/*插入秒杀活动信息*/
	@Override
	public void insert(Rush rush) throws AppBusinessException {
		try {
			getSqlSession()
			.insert("cn.sumpu.app.guoguojie.persistence.RushMapper.insert",
					rush);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insert rushdatabase failure.", e);
			}
			throw new AppBusinessException("inserts rushdatabase failure.", e);
		}
		
	}
	//查询所有的活动计划
	@SuppressWarnings("unchecked")
	@Override
	public List<Rush> selectRushList(Map<String, Object> map)throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.RushMapper.selectRushList",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("selectRushList database failure.", e);
			}
			throw new AppBusinessException("selectRushList database failure.", e);
		}
	}
	//查询所有活动计划总数
	@Override
	public int getRushListCount(Map<String, Object> map)throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne(
					"cn.sumpu.app.guoguojie.persistence.RushMapper.getRushListCount",
					map);
			return (Integer) (obj==null?0:obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getRushListCount database failure.", e);
			}
			throw new AppBusinessException("getRushListCount database failure.", e);
		}
	}
	//更新活动计划
	@Override
	public void update(Rush rush)throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.RushMapper.update",
							rush);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateRush database failure.", e);
			}
			throw new AppBusinessException("updateRush database failure.", e);
		}
	}
	//更新事件
	@Override
	public void updateRushPlan(RushPlan rushPlan)throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.RushMapper.updateRushPlan",
							rushPlan);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateRush database failure.", e);
			}
			throw new AppBusinessException("updateRush database failure.", e);
		}
	}
	
	//删除活动计划
	@Override
	public void delete(String identifier)throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.RushMapper.delete",
							identifier);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("delete database failure.", e);
			}
			throw new AppBusinessException("delete database failure.", e);
		}
	}
	//根据活动Id删除事件
	@Override
	public void deleteRushPlanByRushId(String rushId)throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.RushMapper.deleteRushPlanByRushId",
							rushId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("delete database failure.", e);
			}
			throw new AppBusinessException("delete database failure.", e);
		}
	}
	//根据活动Id删除事件时间
	@Override
	public void deleteRushTimeByRushId(String rushId)throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.RushMapper.deleteRushTimeByRushId",
							rushId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("delete database failure.", e);
			}
			throw new AppBusinessException("delete database failure.", e);
		}
	}
	//根据活动Id删除时间段
	@Override
	public void deleteRushTimePartByRushId(String rushId)throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.RushMapper.deleteRushTimePartByRushId",
							rushId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteRushTimePartByRushId database failure.", e);
			}
			throw new AppBusinessException("deleteRushTimePartByRushId database failure.", e);
		}
	}
	
	//删除事件
	@Override
	public void deleteRushPlan(String identifier)throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.RushMapper.deleteRushPlan",
							identifier);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteRushPlan database failure.", e);
			}
			throw new AppBusinessException("deleteRushPlan database failure.", e);
		}
	}
	//删除事件时间
	@Override
	public void deleteRushTime(String rushPlanId)throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.RushMapper.deleteRushTime",
							rushPlanId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteRushPlan database failure.", e);
			}
			throw new AppBusinessException("deleteRushPlan database failure.", e);
		}
	}
	//删除时间段
	@Override
	public void deleteRushTimePart(String rushPlanId)throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.RushMapper.deleteRushTimePart",
							rushPlanId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteRushTimePart database failure.", e);
			}
			throw new AppBusinessException("deleteRushTimePart database failure.", e);
		}
	}
	//定时发布 删除所有时间段
	@Override
	public void deleteAllTimePart()throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.RushMapper.deleteAllTimePart");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteAllTimePart database failure.", e);
			}
			throw new AppBusinessException("deleteAllTimePart database failure.", e);
		}
	}
	
	
	//查询所有的活动计划
	@SuppressWarnings("unchecked")
	@Override
	public List<RushPlan> selectRushPlanList(String identifier)throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.RushMapper.selectRushPlanList",
					identifier);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("selectRushList database failure.", e);
			}
			throw new AppBusinessException("selectRushList database failure.", e);
		}
	}
	//查所有中奖名单
	@SuppressWarnings("unchecked")
	@Override
	public List<RushPhone> selectAllPhone()throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.RushMapper.selectAllPhone"
					);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("selectAllPhone database failure.", e);
			}
			throw new AppBusinessException("selectAllPhone database failure.", e);
		}
	}
	
	//查询活动
	@Override
	public Rush select(String identifier)throws AppBusinessException{
		try {
			return (Rush)getSqlSession().selectOne(
					"cn.sumpu.app.guoguojie.persistence.RushMapper.select",
					identifier);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("select database failure.", e);
			}
			throw new AppBusinessException("select database failure.", e);
		}
	}
	//添加事件
	@Override
	public void insertRushPlan(RushPlan rushPlan) throws AppBusinessException {
		try {
			getSqlSession()
			.insert("cn.sumpu.app.guoguojie.persistence.RushMapper.insertRushPlan",
					rushPlan);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertRushPlan rushdatabase failure.", e);
			}
			throw new AppBusinessException("insertRushPlan rushdatabase failure.", e);
		}
		
	}
	//记录中奖人信息
	
	@Override
	public void insertRushPhone(RushPhone rushPhone) throws AppBusinessException {
		try {
			getSqlSession()
			.insert("cn.sumpu.app.guoguojie.persistence.RushMapper.insertRushPhone",
					rushPhone);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertRushPhone rushdatabase failure.", e);
			}
			throw new AppBusinessException("insertRushPhone rushdatabase failure.", e);
		}
		
	}
	//添加事件时间
	@Override
	public void insertRushTime(RushTime rushTime) throws AppBusinessException {
		try {
			getSqlSession()
			.insert("cn.sumpu.app.guoguojie.persistence.RushMapper.insertRushTime",
					rushTime);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertRushTime rushdatabase failure.", e);
			}
			throw new AppBusinessException("insertRushTime rushdatabase failure.", e);
		}
		
	}
	//添加分隔时间段
	@Override
	public void insertRushtPart(RushTimePart rushTimePart) throws AppBusinessException {
		try {
			getSqlSession()
			.insert("cn.sumpu.app.guoguojie.persistence.RushMapper.insertRushtPart",
					rushTimePart);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertRushtPart rushdatabase failure.", e);
			}
			throw new AppBusinessException("insertRushtPart rushdatabase failure.", e);
		}
		
	}
	
	//查询活动
	@Override
	public RushPlan selectPlanByPlanId(String rushPlanId)throws AppBusinessException{
		try {
			return (RushPlan)getSqlSession().selectOne(
					"cn.sumpu.app.guoguojie.persistence.RushMapper.selectPlanByPlanId",
					rushPlanId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("selectPlanByPlanId database failure.", e);
			}
			throw new AppBusinessException("selectPlanByPlanId database failure.", e);
		}
	}
	//查询符合规则的活动计划—集合
	@SuppressWarnings("unchecked")
	@Override
	public List<Rush> selectRushForGrid()throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.RushMapper.selectRushForGrid");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("selectRushForGrid database failure.", e);
			}
			throw new AppBusinessException("selectRushForGrid database failure.", e);
		}
	}
	//产询符合一级规则的事件
	@SuppressWarnings("unchecked")
	@Override
	public List<RushPlan> selectRushPlanListForGrid(String rushId)throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.RushMapper.selectRushPlanListForGrid",
					rushId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("selectRushPlanListForGrid database failure.", e);
			}
			throw new AppBusinessException("selectRushPlanListForGrid database failure.", e);
		}
	}
	//时事更新数据库奖品数量
	@Override
	public void updateRushTime(String rushTimeId)throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.RushMapper.updateRushTime",
							rushTimeId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateRushTime database failure.", e);
			}
			throw new AppBusinessException("updateRushTime database failure.", e);
		}
	}
	//更新时间段数量
	@Override
	public void updateRushTimePart(RushTimePart rushTimePart)throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.RushMapper.updateRushTimePart",
							rushTimePart);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateRushTimePart database failure.", e);
			}
			throw new AppBusinessException("updateRushTimePart database failure.", e);
		}
	}
	//定时发布
	@Override
	public void updateTimeCountE()throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.RushMapper.updateTimeCountE");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateTimeCountE database failure.", e);
			}
			throw new AppBusinessException("updateTimeCountE database failure.", e);
		}
	}
	//定时发布/所有计划时间
	@SuppressWarnings("unchecked")
	@Override
	public List<RushTime> selectAllTime()throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.RushMapper.selectAllTime");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("selectAllTime database failure.", e);
			}
			throw new AppBusinessException("selectAllTime database failure.", e);
		}
	}
	//查询手机号是否中过奖
	@SuppressWarnings("unchecked")
	@Override
	public List<RushPhone> selectPhoneList(String phoneNum)throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.RushMapper.selectPhoneList",phoneNum);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("selectPhoneList database failure.", e);
			}
			throw new AppBusinessException("selectPhoneList database failure.", e);
		}
	}
	//按条件查询是否中过奖
	@Override
	public RushPhone selectPhoneWin(Map<String, Object> map)throws AppBusinessException{
		try {
			return (RushPhone)getSqlSession().selectOne(
					"cn.sumpu.app.guoguojie.persistence.RushMapper.selectPhoneWin",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("selectPhoneWin database failure.", e);
			}
			throw new AppBusinessException("selectPhoneWin database failure.", e);
		}
	}
	//查询 特殊中奖规则
	@SuppressWarnings("unchecked")
	@Override
	public List<RushPlan> selectRushPlanList(Map<String, Object> map)throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.RushMapper.selectRushPlanListType",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("selectRushPlanList database failure.", e);
			}
			throw new AppBusinessException("selectRushPlanList database failure.", e);
		}
	}
	
	
	
}
